package design.onekey.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import design.onekey.base.BaseEntity;
import design.onekey.base.PageEntity;
import design.onekey.constant.enums.DelFlagEnum;
import design.onekey.mapper.UserLoginLogMapper;
import design.onekey.model.UserLoginLog;
import design.onekey.service.IUserLoginLogService;
import design.onekey.util.MessageUtil;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * @Description: UserLoginLog服务实现
 * @author
 * @date 2020-08-17 10:05:00
 * @version V1.0
 */
@Service
public class UserLoginLogServiceImpl extends ServiceImpl<UserLoginLogMapper, UserLoginLog> implements IUserLoginLogService {

	/**
	 * @Title: tablePagingQuery
	 * @Description: 表格分页查询
	 * @param @param model
	 * @return BaseEntity
	 * @author
	 * @throws
	 */
	@Override
	public PageEntity tablePagingQuery(UserLoginLog model) {
		PageEntity result = new PageEntity();
		model.setDelFlag(DelFlagEnum.UNDEL.getCode());
		com.github.pagehelper.Page<?> page = PageHelper.startPage(model.getCurrentPage(), model.getPageSize()); //分页条件
		List<UserLoginLog> list = this.baseMapper.searchDataLike(model);
		result.setData(list);
		result.setTotal(page.getTotal());
		result.setCurrentPage(model.getCurrentPage());
		result.setPageSize(model.getPageSize());
		return result;
	}


	/**
	 *<p>Title: getDetail</p>
	 *<p>Description: 获取详情</p>
	 * @author
	 * @param id
	 * @return
	 */
	@Override
	public BaseEntity getDetail(Long id) {
		return this.getById(id);
	}

	/**
	 *<p>Title: insert</p>
	 *<p>Description: 新增数据</p>
	 * @author
	 * @param model
	 * @return
	 */
	@Transactional
	@Override
	public BaseEntity insert(UserLoginLog model) {
		BaseEntity result = new BaseEntity();
		this.save(model);
		result.setStatusMsg(MessageUtil.getValue("info.insert"));
		return result;
	}

	/**
	 *<p>Title: update</p>
	 *<p>Description: 更新数据</p>
	 * @author
	 * @param model
	 * @return
	 */
	@Transactional
	@Override
	public BaseEntity update(UserLoginLog model) {
		BaseEntity result = new BaseEntity();
		this.updateById(model);
		result.setStatusMsg(MessageUtil.getValue("info.update"));
		return result;
	}

	/**
	 *<p>Title: delete</p>
	 *<p>Description: 单条删除数据</p>
	 * @author
	 * @param model
	 * @return
	 */
	@Transactional
	@Override
	public BaseEntity delete(Long id) {
		UserLoginLog userLoginLog = new UserLoginLog();
		userLoginLog.setId(id);
		userLoginLog.setDelFlag(DelFlagEnum.DEL.getCode());
		this.updateById(userLoginLog);
		BaseEntity result = new BaseEntity();
		result.setStatusMsg(MessageUtil.getValue("info.delete"));
		return result;
	}

	/**
	 *<p>Title: deleteBatch</p>
	 *<p>Description: 批量删除数据</p>
	 * @author
	 * @param list
	 * @return
	 */
	@Transactional
	@Override
	public BaseEntity deleteBatch(List<Long> ids) {
		List<UserLoginLog> list = new ArrayList<UserLoginLog>();
		for (int i = 0; i < ids.size(); i++) {
			UserLoginLog userLoginLog = new UserLoginLog();
			userLoginLog.setId(ids.get(i));
			userLoginLog.setDelFlag(DelFlagEnum.DEL.getCode());
			list.add(userLoginLog);
		}
		BaseEntity result = new BaseEntity();
		if (list != null && list.size() > 0) {
			this.updateBatchById(list);
		}
		result.setStatusMsg(MessageUtil.getValue("info.delete"));
		return result;
	}
}
